package com.filebackup.business.impl;

import java.util.ArrayList;
import java.util.List;

import com.filebackup.business.LocalFileService;
import com.filebackup.business.ShareService;
import com.filebackup.dao.LocalFileDao;
import com.filebackup.dao.LocalFolderDao;
import com.filebackup.dao.ShareDao;
import com.filebackup.dao.UserDao;
import com.filebackup.util.RootPathUtil;
import com.filebackup.vo.Vo;
import com.filebackup.vo.impl.LocalFileVo;
import com.filebackup.vo.impl.LocalFolderVo;
import com.filebackup.vo.impl.UserVo;

public class LocalFileServiceImpl implements LocalFileService {

	private LocalFileDao localFileDao;
	private LocalFolderDao localFolderDao;
	private UserDao userDao;

	public List<Vo> findByFolder(int id) {
		// TODO Auto-generated method stub
		return this.getLocalFileDao().findByFolder(id);
	}

	public List<Vo> findRootFile(int id) {
		// TODO Auto-generated method stub
		return this.getLocalFileDao().findRootFile(id);
	}

	public void setLocalFileDao(LocalFileDao localFileDao) {
		this.localFileDao = localFileDao;
	}

	public LocalFileDao getLocalFileDao() {
		return localFileDao;
	}

	public String tracePath(int fileId) {
		// TODO Auto-generated method stub
		String path = "";
		LocalFileVo fvo = (LocalFileVo) this.getLocalFileDao().findByPK(fileId);
		int upperId = fvo.getFolderId();
		if (upperId == 0) {
			path = RootPathUtil.ROOT_PATH + fvo.getFileName();
		} else {
			LocalFolderVo lvo = (LocalFolderVo) this.getLocalFolderDao()
					.findByPK(upperId);
			while (lvo.getUpperId() != 0) {
				path = lvo.getFolderName() + "\\" + path;
				lvo = (LocalFolderVo) this.getLocalFolderDao().findByPK(
						lvo.getUpperId());
			}
			path = RootPathUtil.ROOT_PATH + lvo.getFolderName() + "\\" + path
					+ fvo.getFileName();
		}
		return path;
	}

	public void setLocalFolderDao(LocalFolderDao localFolderDao) {
		this.localFolderDao = localFolderDao;
	}

	public LocalFolderDao getLocalFolderDao() {
		return localFolderDao;
	}

	public void changeFileName(int id, String name) {
		// TODO Auto-generated method stub
		LocalFileVo vo = (LocalFileVo) this.getLocalFileDao().findByPK(id);
		vo.setFileName(name);
		vo.setVersion(vo.getVersion() + 1);
		this.getLocalFileDao().updateVo(vo);
	}

	public Boolean isNewVersion(int id, int version) {
		// TODO Auto-generated method stub
		LocalFileVo vo = (LocalFileVo) this.getLocalFileDao().findByPK(id);
		if (vo.getVersion() == version)
			return true;
		else
			return false;
	}

	public void changeFile(int id) {
		// TODO Auto-generated method stub
		Vo vo = this.getLocalFileDao().findByPK(id);
		((LocalFileVo) vo).setVersion(((LocalFileVo) vo).getVersion() + 1);

		System.out.println("-------- file: " + id + " the new version:"
				+ ((LocalFileVo) vo).getVersion());
		this.getLocalFileDao().updateVo(vo);
	}

	public void deleteFile(int id) {
		// TODO Auto-generated method stub
		LocalFileVo vo = (LocalFileVo) this.getLocalFileDao().findByPK(id);
		this.getLocalFileDao().deleteVo(vo);

	}

	public int addFile(LocalFileVo vo) {
		// TODO Auto-generated method stub
		this.getLocalFileDao().addVo(vo);
		return 0;
	}

	public List<Vo> findFileByUser(int id) {
		// TODO Auto-generated method stub
		return this.getLocalFileDao().findByOwner(id);
	}

	public List<Vo> findAllSubFile(int id) {
		// TODO Auto-generated method stub
		LocalFolderVo vo = (LocalFolderVo) this.getLocalFolderDao()
				.findByPK(id);
		List<Vo> voList = new ArrayList<Vo>();
		List<Vo> lvoList = this.getLocalFolderDao().findByUpperId(vo.getId());
		List<Vo> fvoList = this.getLocalFileDao().findByFolder(vo.getId());
		for (int i = 0; i < lvoList.size(); i++) {
			voList.addAll(findAllSubFile(((LocalFolderVo) lvoList.get(i))
					.getId()));
		}
		for (int i = 0; i < fvoList.size(); i++) {
			voList.add(fvoList.get(i));
		}
		return voList;
	}

	public int findNewId(LocalFileVo vo) {
		// TODO Auto-generated method stub
		return this.getLocalFileDao().findIdByVo(vo);
	}

	public String returnPath(int fileId) {
		// TODO Auto-generated method stub
		String path = "";
		LocalFileVo fvo = (LocalFileVo) this.getLocalFileDao().findByPK(fileId);
		int upperId = fvo.getFolderId();
		if (upperId == 0) {
			path = fvo.getFileName();
		} else {
			LocalFolderVo lvo = (LocalFolderVo) this.getLocalFolderDao()
					.findByPK(upperId);
			while (lvo.getUpperId() != 0) {
				path = lvo.getFolderName() + "\\" + path;
				lvo = (LocalFolderVo) this.getLocalFolderDao().findByPK(
						lvo.getUpperId());
			}
			path = lvo.getFolderName() + "\\" + path + fvo.getFileName();
		}
		return path;
	}

	public String findNameById(int id) {
		// TODO Auto-generated method stub
		return ((LocalFileVo)this.getLocalFileDao().findByPK(id)).getFileName();
	}

	public UserVo findOwnerById(int id) {
		// TODO Auto-generated method stub
		LocalFileVo vo=(LocalFileVo)this.getLocalFileDao().findByPK(id);
		UserVo uvo=(UserVo)this.getUserDao().findByPK(vo.getOwnerId());
		return uvo;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

}
